layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 拼单管理
     */
    var Merge = {
        tableId: "mergeTable"
    };

    /**
     * 初始化表格的列
     */
    Merge.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id',align:'center'},
            {field: 'mergeNo', sort: true, title: '拼单号',align:'center'},
            {field: 'goodsName', sort: true, title: '商品',event:"goodsDetail",align:'center',
                templet:function (d) {
                    return '<span style="color: #01AAED;">'+ d.goodsName +'</span>'
                }
            },
            {field: 'playerAmount', sort: true, title: '当前拼单人数',event:"participatantDetail",align:'center',
                templet:function (d) {
                    return '<span style="color: #01AAED;">'+ d.playerAmount +'</span>'
                }
            },
            {field: 'status', sort: true, title: '拼单状态',align:'center',
                templet:function (d) {
                    var obj = [["0","拼单中"],["1","成功"],["2","失败"]];
                    var matchStatus = Feng.MatchStatus(obj,d.status,"未知");
                    return Feng.addBackColor4Enum(d.status,matchStatus);
                }
            },
            {field: 'endTime', sort: true, title: '结束时间',align:'center'},
            {field: 'createTime', sort: true, title: '生成时间',align:'center'},
            {field: 'updateTime', sort: true, hide: true, title: '修改时间'},
            {field: 'recordStatus', sort: true, hide: true, title: '记录状态'},
          /*  {align: 'center', toolbar: '#tableBar', title: '操作'}*/
        ]];
    };
    /**
     * 点击查询按钮
     */
    Merge.search = function () {
        var queryData = {};
        queryData['goodsNo'] = $("#goodsNo").val();
        queryData['goodsName'] = $("#goodsName").val();
        queryData['status'] = $("#status").val();
        queryData['mergeNo'] = $("#mergeNo").val();
        table.reload(Merge.tableId, {where: queryData,
            page:{curr:1}
        });
    };

    /**
     * 弹出添加对话框
     */
    Merge.openAddDlg = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加拼单',
            content: Feng.ctxPath + '/merge/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(Merge.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    Merge.exportExcel = function () {
        var checkRows = table.checkStatus(Merge.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    Merge.openEditDlg = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '修改拼单',
            content: Feng.ctxPath + '/merge/edit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Merge.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Merge.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/merge/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Merge.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Merge.tableId,
        url: Feng.ctxPath + '/merge/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Merge.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Merge.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Merge.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Merge.exportExcel();
    });


    /**
     * 点击查看详情
     * @param data
     */

    Merge.openGoodsDetailDlg = function (data) {
        admin.putTempData('formOk', false);
        admin.open({
            type: 2,
            title: '商品信息',
            anim: 1,
            area:["100%","100%"],
            maxmin:'true',
            content: Feng.ctxPath + '/goods/goodsDetail?id=' + data.goodsId,
            end: function () {
                admin.getTempData('formOk') && table.reload(Merge.tableId);
            }
        });
    };


    /**
     * 查看拼单人员
     *
     * @param data 点击按钮时候的行数据
     */
    Merge.openParticipantEditDlg = function (data) {
        admin.putTempData('formOk', false);
        layui.admin.open({
            type: 2,
            maxmin:true,
            area:["100%","100%"],
            title: '拼单参与者',
            content: Feng.ctxPath + '/mergeParticipant/?mergeId=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Merge.tableId);
            }
        });
    };


    // 工具条点击事件
    table.on('tool(' + Merge.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Merge.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Merge.onDeleteItem(data);
        }else if (layEvent === 'goodsDetail') {
            if(data.goodsName=="商品已下架"){
                return false;
            }
            Merge.openGoodsDetailDlg(data);
        }else if (layEvent === 'participatantDetail') {
            if(data.playerAmount=="0"){
                return false;
            }
            Merge.openParticipantEditDlg(data);
        }
    });
});
